package models

import (
	"kop/libs/database"
	"kop/modules/building/entity"
)

func GetDispatchList(roleID int32) []entity.Dispatch {

	var data []entity.Dispatch
	if err := database.DB.Where("role_id = ?", roleID).Find(&data).Error; err != nil {
		panic(err)
	}

	return data
}

func GetDispatch(roleID, dispatchID int32) entity.Dispatch {

	var data = entity.Dispatch{RoleID: roleID, DispatchID: dispatchID}
	if err := database.DB.First(&data).Error; err != nil {
		panic(err)
	}

	return data
}

func CreateDispatch(roleID, dispatchID, peerage int32) entity.Dispatch {

	var data = entity.Dispatch{RoleID: roleID, DispatchID: dispatchID, Peerage: peerage}
	if err := database.DB.Create(&data).Error; err != nil {
		panic(err)
	}

	return data
}

func SaveDispatch(data *entity.Dispatch) error {

	return database.DB.Save(data).Error
}

func DelDispatch(data entity.Dispatch) error {

	return database.DB.Delete(&data).Error
}

func DelDispatchByRoleID(roleID int32) error {

	return database.DB.Where("role_id = ? AND ship_id = 0", roleID).Delete(&entity.Dispatch{}).Error
}
